import { Injectable } from '@angular/core';
import {
  CanActivate, Router,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
  CanActivateChild,
} from '@angular/router';
import { NbAuthService } from '@nebular/auth';
import { tap } from 'rxjs/operators';

@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {

  constructor(
    private authService: NbAuthService,
    private router: Router
  ) {}

  canActivate() {
    // return this.authService.isAuthenticated()
    //             .pipe(
    //               tap(authenticated => {
    //                 if (!authenticated) {
    //                   this.router.navigate(['auth/ngx-login']);
    //                 }
    //               })
    //             )
    return localStorage.getItem('auth_app_token') !== null ||
           sessionStorage.getItem('auth_app_token') !== null
  }

  canActivateChild(
    route: ActivatedRouteSnapshot, state: RouterStateSnapshot
  ) {
    return this.canActivate();
  }
}
